package org.dftproject.genesis.core.collections;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dftproject.genesis.core.Activator;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;

import com.eekbom.utils.Strings;

public class Collection implements Comparable<Collection> {

	private static final Log log = LogFactory.getLog(Collection.class);

	private String id;
	private String name;
	private String description;
	private String type;
	private String data;
	private Date lastAccessed;
	
	public Collection(String id, String name, String description, String type, String data, Date lastAccessed) {
		this.id = id;
		this.name = name;
		this.description = description;
		this.type = type;
		this.data = data;
		this.lastAccessed = lastAccessed;
	}
	
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public int compareTo(Collection other) {
		int c = Strings.compareNatural(name, other.getName());
		if (c != 0)
			return c;
		return Strings.compareNatural(description, other.getDescription());
	}

	public Date getLastAccessed() {
		return lastAccessed;
	}
	
	public void setLastAccessed(Date lastAccessed) {
		this.lastAccessed = lastAccessed;
	}
	
	public ICollectionDataProvider getDataProvider() {
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint extensionPoint = registry.getExtensionPoint(Activator.ID, Activator.COLLECTION_DATA_PROVIDER_FACTORIES_EXTENSION_POINT);
		IExtension[] extensions = extensionPoint.getExtensions();
		for (int i = 0; i < extensions.length; i++) {
			IConfigurationElement[] configElements = extensions[i].getConfigurationElements();
			for (int j = 0; j < configElements.length; j++) {
				if (type.equals(configElements[j].getAttribute("type"))) {
					try {
						Object dataProviderFactory = configElements[j].createExecutableExtension("class");
						if (dataProviderFactory instanceof ICollectionDataProviderFactory)
							return ((ICollectionDataProviderFactory) dataProviderFactory).createProvider(this);
					} catch (CoreException e) {
						log.error("Failed to create collection data provider factory", e);
					}
				}
			}
		}
		
		return null;
	}
	
}
